package com.riuzzang.android.callmebaby.util;

import java.util.ArrayList;
import java.util.HashMap;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.riuzzang.android.callmebaby.R;

public class CustomListAdapter extends ArrayAdapter<HashMap<String, Object>> {
    
	private ArrayList<HashMap<String, Object>> data;
	private Context context;
	
	public HashMap<String,String> checked = new HashMap<String,String>();
	
	public CustomListAdapter(Context context, int textViewResourceId, ArrayList<HashMap<String, Object>> data) {
		//let android do the initializing :)
		super(context, textViewResourceId, data);
		
		this.data = data;
		this.context = context;
	}

	//class for caching the views in a row  
	private class ViewHolder {
		ImageView type;
		TextView ch_name;
		TextView ch_filepath;
		TextView ch_dtfull;
	}
	
	public void setCheckedItem(int item) {
        if (checked.containsKey(String.valueOf(item))){
            checked.remove(String.valueOf(item));
        }

        else {
            checked.put(String.valueOf(item), String.valueOf(item));
        }
    }
    
	public HashMap<String, String> getCheckedItems(){
        return checked;
    }
  
	ViewHolder viewHolder;

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
  
		if(convertView == null) {        
                               //inflate the custom layout                        
			convertView = ((LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.list_item, null);
			
			viewHolder = new ViewHolder();
    
			//cache the views
			viewHolder.type = (ImageView)convertView.findViewById(R.id.type);
			viewHolder.ch_name = (TextView)convertView.findViewById(R.id.ch_name);
			viewHolder.ch_filepath = (TextView)convertView.findViewById(R.id.ch_filepath);
			viewHolder.ch_dtfull = (TextView)convertView.findViewById(R.id.ch_dtfull);
    
			// link the cached views to the convertview
			convertView.setTag(viewHolder);
		}else{
			viewHolder=(ViewHolder) convertView.getTag();
		}
   
  
		String ch_type = StringManager.getStr(this.data.get(position).get("ch_type"));
   
		Drawable type = null;
		
		if(ch_type.equals("incoming")){
			type = this.context.getResources().getDrawable(android.R.drawable.sym_call_incoming);
		}else if(ch_type.equals("outgoing")){
			type = this.context.getResources().getDrawable(android.R.drawable.sym_call_outgoing);
		}else{
			type = this.context.getResources().getDrawable(android.R.drawable.sym_action_call);
		}
		
		//set the data to be displayed
		viewHolder.type.setImageDrawable(type);
		
		String chName = StringManager.getStr(this.data.get(position).get("ch_name"));
		String chFilepath = StringManager.getStr(this.data.get(position).get("ch_filepath"));
		String chDtfull = StringManager.getStr(this.data.get(position).get("ch_dtfull"));
		
		viewHolder.ch_name.setText(chName);
		viewHolder.ch_filepath.setText(chFilepath);
		viewHolder.ch_dtfull.setText(chDtfull);
  
		//return the view to be displayed
		return convertView;
	}
      
}
